#!/bin/sh
# 编译安装管理Redis
App=redis
AppName=Redis
AppBase=/App
AppDir=$AppBase/$App
AppProg=$AppDir/bin/redis-server
AppConf=$AppDir/redis.conf

AppSrcBase=/App/src
AppSrcFile=$App-*.tar.*
AppSrcDir=$(find $AppSrcBase -maxdepth 1 -name "$AppSrcFile" -type f 2> /dev/null | sed -e 's/.tar.*$//' -e 's/^.\///')

RemoveFlag=0
InstallFlag=0

# 获取PID
fpid()
{
    AppPid=$(ps ax | grep "$AppProg" | grep -v "grep" | awk '{print $1}' 2> /dev/null)
}

# 查询状态
fstatus()
{
    fpid

    if [ ! -f "$AppProg" ]; then
        echo "$AppName 未安装"
    else
        echo "$AppName 已安装"
        if [ -z "$AppPid" ]; then
            echo "$AppName 未启动"
        else
            echo "$AppName 正在运行"
        fi
    fi
}

# 删除
fremove()
{
    fpid
    RemoveFlag=1

    if [ -z "$AppPid" ]; then
        if [ -d "$AppDir" ]; then
            rm -rf $AppDir && echo "删除 $AppName"
        else
            echo "$AppName 未安装"
        fi
    else
        echo "$AppName 正在运行" && exit
    fi
}

# 备份
fbackup()
{
    Day=$(date +%Y-%m-%d)
    BackupFile=$App-$Day.tgz

    if [ -f "$AppProg" ]; then
        cd $AppBase
        tar zcvf $BackupFile --exclude=logs/* $App --backup=numbered
        [ $? -eq 0 ] && echo "$AppName 备份成功" || echo "$AppName 备份失败"
    else
        echo "$AppName 未安装" 
    fi
}

# 安装
finstall()
{
    fpid
    InstallFlag=1

    if [ -z "$AppPid" ]; then
        test -f "$AppProg" && echo "$AppName 已安装"
        [ $? -ne 0 ] && fupdate && fcpconf
    else
        echo "$AppName 正在运行"
    fi
}

# 拷贝配置
fcpconf()
{
    cp -vf --backup=numbered $ScriptDir/redis.conf $AppDir
}

# 更新
fupdate()
{
    Operate="更新"
    [ $InstallFlag -eq 1 ] && Operate="安装"
    [ $RemoveFlag -ne 1 ] && fbackup

    cd $AppSrcBase
    test -d "$AppSrcDir" && rm -rf $AppSrcDir

    tar zxf $AppSrcFile || tar jxf $AppSrcFile
    cd $AppSrcDir
    make PREFIX=$AppDir install

    if [ $? -eq 0 ]; then
        echo "$AppName $Operate成功"
    else
        echo "$AppName $Operate失败"
        exit 1
    fi
}

# 启动
fstart()
{
    fpid

    if [ -n "$AppPid" ]; then
        echo "$AppName 正在运行"
    else
        $AppProg $AppConf && echo "启动 $AppName" || echo "$AppName 启动失败"
    fi
}

# 停止
fstop()
{
    fpid

    if [ -n "$AppPid" ]; then
        $AppDir/bin/redis-cli shutdown && echo "停止 $AppName" || echo "$AppName 停止失败"
    else
        echo "$AppName 未启动"
    fi
}

# 重启
frestart()
{
    fpid
    [ -n "$AppPid" ] && fstop && sleep 1
    fstart
}

# 终止进程
fkill()
{
    fpid

    if [ -n "$AppPid" ]; then
        kill -9 $AppPid && echo "终止 $AppName 进程" || echo " 终止 $AppName 进程失败"
    else
        echo "$AppName 进程未运行"
    fi
}


ScriptDir=$(dirname $0)
ScriptFile=$(basename $0)
case "$1" in
    "install"   ) finstall;;
    "update"    ) fupdate;;
    "reinstall" ) fremove && finstall;;
    "remove"    ) fremove;;
    "backup"    ) fbackup;;
    "start"     ) fstart;;
    "stop"      ) fstop;;
    "restart"   ) frestart;;
    "status"    ) fstatus;;
    "cpconf"    ) fcpconf;;
    "kill"      ) fkill;;
    *           )
    echo "$ScriptFile install              安装 $AppName"
    echo "$ScriptFile update               更新 $AppName"
    echo "$ScriptFile reinstall            重装 $AppName"
    echo "$ScriptFile remove               删除 $AppName"
    echo "$ScriptFile backup               备份 $AppName"
    echo "$ScriptFile start                启动 $AppName"
    echo "$ScriptFile stop                 停止 $AppName"
    echo "$ScriptFile restart              重启 $AppName"
    echo "$ScriptFile status               查询 $AppName 状态"
    echo "$ScriptFile cpconf               拷贝 $AppName 配置"
    echo "$ScriptFile kill                 终止 $AppName 进程"
    ;;
esac
